<?php

/*
	Class: Modules

	About: Author
		J. William McCarthy

	About: License
		<http://communit.as/docs/license>

	About: See Also
	 	<App_Db_Table_Abstract>
*/
class Modules extends App_Db_Table_Abstract {
    protected $_name 	= 'default_modules';
    protected $_primary = 'id';
    public $errors 		= null; 
	public $success 	= null;
    public $notice 		= null;   

	/* Group: Has No Methods */
	
	
	function exists($module_id){
		$where = $this->getAdapter()->quoteInto("id = ?",$module_id);
		if($this->getCountByWhereClause($where) > 0){
			return true;	
		} else {
			return false;
		}
	}
	
	function getEnabledModules($include_default = true){
		$enabled_modules = array();
		$modules = $this->fetchAll("is_enabled = 1");
		if(count($modules) > 0){
			foreach($modules as $module){
				$enabled_modules[] = $module->id;
			}
		}
		
		if($include_default){
			$enabled_modules[] = "default";
		}
		return $enabled_modules;
	}
	
	function isEnabled($module_id){
		$where = $this->getAdapter()->quoteInto("id = ? and is_enabled = 1",$module_id);
		if($this->getCountByWhereClause($where) > 0){
			return true;	
		} else {
			return false;
		}
	}
	
	function enable($module_id){
		if($this->runFilter($module_id."_enable")){
			$where = $this->getAdapter()->quoteInto("id = ?",$module_id);
			$data['is_enabled'] = 1;	
			$this->update($data,$where);
			return true;
		} else {
			return false;
		}
	}
	
	function disable($module_id){
		if($this->runFilter($module_id."_disable")){
			$where = $this->getAdapter()->quoteInto("id = ?",$module_id);
			$data['is_enabled'] = 0;	
			$this->update($data,$where);
			return true;
		} else {
			return false;
		}
	}
	
	function install($module_id){
		$this->setup($module_id);
		if($this->runFilter($module_id."_install")){
			$data = array("id" => $module_id);
			$this->insert($data);
			return true;
		} else {
			return false;
		}
	}
	
	function uninstall($module_id){	
		$this->setup($module_id);
		if($this->runFilter($module_id."_uninstall")){	
			$where = $this->getAdapter()->quoteInto("id = ?",$module_id);		
			$this->delete($where);
			return true;
		} else {
			return false;
		}
	}
	
		
	private function runFilter($filter_name){
		$params = array(
			"errors" => array(),
			"success" => null,
			"notice" => null,
		);
		$params = $this->_app_plugin->doFilter($filter_name, $params);
		$this->success = $params['success'];
		$this->notice = $params['notice'];
		$this->errors = $params['errors'];
		if(count($params['errors']) > 0){			
			return false;
		} else {
			return true;
		}		
	}
	
	function parseIni($module_id){
		$basepath = Zend_Registry::get("basepath");
		$module_ini_file = $basepath . "/modules/".$module_id."/module.ini";
		$config = array();
		if(file_exists($module_ini_file)){
			$this->_logger->report("found module.ini for ". $module_id,null,Zend_Log::INFO);
			$config = parse_ini_file($module_ini_file,true);
			if($config === false){
				$this->_logger->report("module.ini for ". $module_id . " is corrupt",null,Zend_Log::ERR);
			} else {
				$this->_logger->report("module.ini for ". $module_id ,$config,Zend_Log::DEBUG);				
			}
		} else {
			$this->_logger->report("can't find module.ini for ". $module_id,null,Zend_Log::ERR);
		}
		return $config;
	}
	
	function setup($module_id){
		$basepath = Zend_Registry::get("basepath");
		$module_dir = $basepath."/modules";
		$full_dir = $module_dir . "/" . $module_id;
		$subdirs = array("models","plugins","controllers","lib");
		$tmp_include_path = "";
		$ap = new App_Plugin();
		
		if (is_dir($full_dir)) {
		   	foreach($subdirs as $subdir){
				$includable_dir = $full_dir ."/".$subdir;
				if(is_dir($includable_dir)){
					$tmp_include_path .= PATH_SEPARATOR . $includable_dir;
				}
				
			}
			set_include_path(get_include_path() . $tmp_include_path);
		}
		
		$module_cfg = $this->parseIni($module_id);	   			    
	   			    
	    if(count($module_cfg['plugins']) > 0){
	    	foreach($module_cfg['plugins'] as $hook => $plugin){
	    		$hook_type = substr($hook,0,strpos($hook,"."));
	    		$hook_name = substr($hook,strpos($hook,".") + 1);
	    		$callback_class = substr($plugin,0,strpos($plugin,"::"));
	    		$callback_method = substr($plugin,strpos($plugin,"::") + 2);			    		
	    		
	    		if($hook_type == "filter"){	   			    			
	    			$ap->addFilter($hook_name,$callback_class,$callback_method,10);
	    		}
	    		
	    		if($hook_type == "action"){	   			    			
	    			$ap->addAction($hook_name,$callback_class,$callback_method,10);
	    		}
	    		
	    	}
	    	
	    }		
		
	}
	
}
